var userId = getvl("userId");
$(function(){
	     if(userId!=''){
	       	$("#xinzheng").html("修改地址");
	     		xinzhengdizhi(userId);
	     }
});

function xinzhengdizhi(){
		$("#aid").val("");
	$.ajax({
		url: urlcore + "/api/address/selectOne?id="+userId,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
			 	 var  da=data.data;
			 	 $("#xiugai").html("");
			 	 $("#lainxiren").val("");
			 	 $("#shoujihao").val("");
			 	 $("#cityResult3").html("");//******
			 	 $("#xiangxidizhi").val("");
			 	 $("#lainxiren").val(da.userName);
			 	 $("#shoujihao").val(da.userPhone);
			 	 $("#cityResult3").html(da.province+" "+da.city+" "+da.county);
			 	$("#xiangxidizhi").val(da.address);
			 	$("#aid").val(da.id);
			 	if(da.level==1){
			 		 $('#checkbox-1-1').attr('checked','checked');
			 	}
			 $("#xiugai").html('<input type="button" class="p_button" onclick="xiugai()" value="修改地址">');
			}
		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});	

}

function xiugai(){
		 var lainxiren=$("#lainxiren").val();
	 var shoujihao=$("#shoujihao").val();
	 var xiangxidizhi=$("#xiangxidizhi").val();
	 var aids=$("#aid").val();
	 var level=$("#level").val();
 var cityResult3=$("#cityResult3").html();
	 var imgarr2 = cityResult3.split(" ");
	 var imgStr3="";
	 var imgStr4="";
	 var imgStr5="";
	  $.each(imgarr2, function(index, value) {
							if(index==1){
								imgStr3=value;
								
							}else if(index==2){
								imgStr4=value;
								
							}else{
								
								imgStr5=value;
							}
						});
	    $.ajax({  
        url : urlcore + "/api/address/update",
        type : "post",  
        contentType: "application/json;charset=utf-8",
        data :  JSON.stringify({  //所在省市
        	  "id":aids,
            "userName":lainxiren,
            "userPhone":shoujihao,
            "province":imgStr3,
            "city":imgStr4,
            "county":imgStr5,
            "address":xiangxidizhi,
            "level":level
        	}),
        	dataType: "json",
        success: function(data) {//data是返回的hash,key之类的值，key是定义的文件名 
        	//alert("修改成功!");
        	window.location.href = document.referrer
        },
        error:function(){  
            alert("修改失败");  
        }  
    });     
	      
	
}




function baochun(){
	if(uuuuid!=''){
	 var lainxiren=$("#lainxiren").val();
	 var shoujihao=$("#shoujihao").val();
	 var xiangxidizhi=$("#xiangxidizhi").val();
	 var level=$("#level").val();
 var cityResult3=$("#cityResult3").html();
	 var imgarr2 = cityResult3.split(" ");
	 var imgStr3="";
	 var imgStr4="";
	 var imgStr5="";
	  $.each(imgarr2, function(index, value) {
							if(index==1){
								imgStr3=value;
								
							}else if(index==2){
								imgStr4=value;
								
							}else{
								
								imgStr5=value;
							}
						});
	    $.ajax({  
        url : urlcore + "/api/address/addupdatedizhi",
        type : "post",  
        contentType: "application/json;charset=utf-8",
        data :JSON.stringify({  //所在省市
        	  "userId":uuuuid,
            "userName":lainxiren,
            "userPhone":shoujihao,
            "province":imgStr3,
            "city":imgStr4,
            "county":imgStr5,
            "address":xiangxidizhi,
            "level":level
        	}),
        	dataType: "json",
        success: function(data) {//data是返回的hash,key之类的值，key是定义的文件名 
        	//alert("保存成功!");
        	window.location.href = document.referrer
        },
        error:function(){  
            alert("保存失败");  
        }  
    });
    }else{
    	window.location.href="1.0.0denglu.html";
    }
}
